package com.baijiayun.liveuibase.utils;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.baijiayun.livecore.utils.ToastCompat;
import com.baijiayun.liveuibase.R;
import com.baijiayun.liveuibase.utils.drawable.DrawableBuilder;

/**
 * @author lzd
 * 小班统一 toast 工具类
 */
public class ToastUtil {private Context context;
    private String mainText;
    private Drawable icon;
    private int duration = Toast.LENGTH_SHORT;

    private Toast toast;

    public ToastUtil(Context context) {
        this.context = context;
    }

    /**
     * 设置主提示信息
     */
    public ToastUtil setText(String text) {
        mainText = text;
        return this;
    }

    /**
     * 设置 icon
     */
    public ToastUtil setIcon(Drawable icon) {
        this.icon = icon;
        return this;
    }

    /**
     * 设置 Duration ，默认为 short
     */
    public ToastUtil setDuration(int duration) {
        if (duration == Toast.LENGTH_LONG || duration == Toast.LENGTH_SHORT) {
            this.duration = duration;
        } else {
            this.duration = Toast.LENGTH_SHORT;
        }
        return this;
    }

    public ToastUtil create() {
        toast = new Toast(context);
        View contentView = LayoutInflater.from(context).inflate(R.layout.uibase_common_toast_layout, null);
        toast.setView(contentView);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.setDuration(duration);

        ImageView ivIcon = contentView.findViewById(R.id.base_common_toast_icon);
        ivIcon.setVisibility(icon == null ? View.GONE : View.VISIBLE);
        ivIcon.setImageDrawable(icon);

        TextView tvText = contentView.findViewById(R.id.base_common_toast_text);
        tvText.setText(mainText);

        View container = contentView.findViewById(R.id.base_common_toast_main_container);
        container.setBackground(new DrawableBuilder()
                .solidColor(ThemeDataUtil.getColorFromThemeConfigByAttrId(context, R.attr.base_theme_brand_container_color))
                .cornerRadius(context.getResources().getDimensionPixelSize(R.dimen.bjy_base_common_bg_radius))
                .build());

        ShadowUtil.setViewBoundShadow(contentView.findViewById(R.id.base_common_toast_shadow_container));
        return this;
    }

    public void show() {
        if (toast != null) {
            if (Build.VERSION.SDK_INT == Build.VERSION_CODES.N_MR1) {
                ToastCompat.hook(toast);
            }
            toast.show();
        }
    }
}
